package com.rrjjg.app.admin.data.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rrjjg.app.admin.bus.bo.UserBo;
import com.rrjjg.app.admin.data.service.IRoleService;
import com.rrjjg.app.admin.data.service.IUserService;
import com.rrjjg.app.admin.data.storage.entity.Role;
import com.rrjjg.app.admin.data.storage.entity.User;
import com.rrjjg.app.admin.data.storage.mapper.UserMapper;
import com.rrjjg.app.core.exception.RjException;
import com.rrjjg.app.core.sign.JwtHelper;
import com.rrjjg.app.core.utils.StringUtil;
import com.rrjjg.app.mybatis.bean.PageData;
import com.rrjjg.app.mybatis.util.PageUtil;
import lombok.val;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.rrjjg.app.admin.module.Constants.*;
import static com.rrjjg.app.admin.module.enums.ErrorCode.LOGIN_ERROR;
import static com.rrjjg.app.core.Constants.JWT_BODY;
import static com.rrjjg.app.core.Constants.JWT_HEADER;

/**
 * <p>
 * 用户 服务实现类
 * </p>
 *
 * @author vayne
 * @since 2023-04-06
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    JwtHelper<Map<String,Object>> jwtHelper;
    @Autowired
    IRoleService iRoleService;

    @Override
    public PageData<UserBo> queryPage(User user, int page, int size) {
        Page<UserBo> userDtoPage = getBaseMapper().pageQuery(new Page<>(page, size),PageUtil.transformation(user, UserBo::new) );
        return PageUtil.EtoVList(userDtoPage);
    }

    @Override
    public String login(User user) {
        List<User> list = list(PageUtil.queryWrapper(user, User.class));
        if (CollectionUtils.isEmpty(list)) {
            throw new RjException(LOGIN_ERROR.getMsg(), LOGIN_ERROR.getCode());
        }
        User u = list.get(0);
        String [] roleName = new String[1] ;
        Long [] roleId= new Long[1] ;
        if (u.getRoleId() != null) {
           Role role = iRoleService.getById(u.getRoleId());
           if(role!=null){
               roleName[0] = role.getRoleName();
               roleId[0] = role.getId();
           }
        }
        return jwtHelper.doCreate(jwtMap(u, roleName[0],roleId[0]));
    }


    @NotNull
    private static HashMap<String, Object> jwtMap(User u, String roleName,Long roleId) {
        val map = new HashMap<String,Object>();
        //头信息
        val header = new HashMap<>();
        header.put(CONFIG_TOKEN_HEADER_KEY,StringUtil.uuid());
        map.put(JWT_HEADER, header);
        //消息体
        val info = new HashMap<String, Object>();
        info.put(CONFIG_TOKEN_BODY_NAME, u.getLoginName());
        info.put(CONFIG_TOKEN_BODY_ROLE, roleName);
        info.put(CONFIG_TOKEN_BODY_ROLE_ID, roleId);
        info.put(CONFIG_TOKEN_BODY_ID, u.getId());
        map.put(JWT_BODY,info);
        return map;
    }

    @Override
    public Map<String, Object> info() {
        return jwtHelper.get(null);
    }

}
